﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSWXBLPDA
{
    public partial class Login : Form
    {
        private LoginReference.Registration reg = new BSWXBLPDA.LoginReference.Registration();
        public Login()
        {
            InitializeComponent();
            tb_userid.Focus();
        }


        private void tb_userid_KeyDown(object sender, KeyEventArgs e)
        {                          
            if (e.KeyCode == Keys.Enter)
            {
                string userid = this.tb_userid.Text.ToString().Trim();
                string id = "";
                string username = "";
                bool lb_result = false;
                if (userid == null || userid == "")
                {
                    MessageBox.Show("请扫描用户条码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                lb_result = reg.GetUserInfoByUserID(userid, out id, out username);

                if (lb_result == true)
                {
                    Program.user = username;
                    Program.ID = id;
                    Main iw = new Main(Program.ID, Program.user);
                    iw.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("扫码登录失败!请重新扫码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                }
            }
               
        }

        //登录
        private void button1_Click(object sender, EventArgs e)
        {

            string username = tbName.Text.ToString().Trim();
            string password = tbPassword.Text.ToString().Trim();
            string id = "";

            if (username == null || username == "")
            {
                MessageBox.Show("用户名不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
                //username = "admin";
            }
            if (password == "" || password == null)
            {
                MessageBox.Show("密码不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
                //password = "123456";
            }

            bool ls_return = reg.GetUserInfoByName(username, password, out id);
            if (ls_return == true)
            {
                Program.user = username;
                Program.ID = id;
                Main iw = new Main(Program.ID, Program.user);
                iw.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("登录失败,请正确填写用户名密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

                tbName.Text = "";
                tbPassword.Text = "";
                
            }
            if (id == "-1")
            {
                MessageBox.Show("用户名或密码错误,请重新填写！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                tbName.Text = "";
                tbPassword.Text = "";
                
            }

        }
        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}